/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import main.ConfigGeral;
import entities.Teatro;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

/**
 *
  * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class RmiClient {

    ReceiveMessageInterface rmiServer;
    Registry registry;
    String serverAddress = ConfigGeral.CG_SERVER_APP_IP;
    int serverPort = ConfigGeral.CG_SERVER_APP_PORT;
    String serverName = ConfigGeral.CG_SERVER_APP_NAME;
    String text;
    
    
    public ArrayList<String> getLista(){
        
        ArrayList<String> lista = null;
        
        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    serverPort);
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup(serverName));
            
            // call the remote method
           lista = rmiServer.listTheaters();
            

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        return lista;
        
    }
    
    public Teatro getTeatro(String teatroNome){
        
        Teatro teatro = null;
        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    serverPort);
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup(serverName));
            
            // call the remote method
           teatro = rmiServer.getTheater(teatroNome);
            

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        return teatro;
    }

    public String start() {

        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    serverPort);
            System.out.println("meu registo:" + registry.list());

            // look up the remote object
            rmiServer =(ReceiveMessageInterface) (registry.lookup(serverName));
            
            // call the remote method
            Teatro teatro = rmiServer.getTheater("teatro12");
            return teatro.nome;

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
        
        return "nada";
    }
}
